package com.example.service.impl;

import com.example.dao.PostLikeMapper;
import com.example.dao.CommentLikeMapper;
import com.example.domain.PostLike;
import com.example.domain.CommentLike;
import com.example.service.LikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LikeServiceImpl implements LikeService {

    @Autowired
    private PostLikeMapper postLikeMapper;

    @Autowired
    private CommentLikeMapper commentLikeMapper;

    @Override
    public void togglePostLike(Integer userId, Integer postId) {
        if (isPostLiked(userId, postId)) {
            // 已点赞，取消点赞
            postLikeMapper.removeLike(userId, postId);
        } else {
            // 未点赞，添加点赞
            PostLike postLike = new PostLike(userId, postId);
            postLikeMapper.addLike(postLike);
        }
    }

    @Override
    public boolean isPostLiked(Integer userId, Integer postId) {
        return postLikeMapper.isLiked(userId, postId) > 0;
    }

    @Override
    public int getPostLikeCount(Integer postId) {
        return postLikeMapper.getLikeCount(postId);
    }

    @Override
    public void toggleCommentLike(Integer userId, Integer commentId) {
        if (isCommentLiked(userId, commentId)) {
            // 已点赞，取消点赞
            commentLikeMapper.removeLike(userId, commentId);
        } else {
            // 未点赞，添加点赞
            CommentLike commentLike = new CommentLike(userId, commentId);
            commentLikeMapper.addLike(commentLike);
        }
    }

    @Override
    public boolean isCommentLiked(Integer userId, Integer commentId) {
        return commentLikeMapper.isLiked(userId, commentId) > 0;
    }

    @Override
    public int getCommentLikeCount(Integer commentId) {
        return commentLikeMapper.getLikeCount(commentId);
    }
}
